﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.Security;
 

namespace WirtualneNauczanie.Web
{
    public partial class CourseTest : System.Web.UI.Page
    {
        String yourcourseid, section;
        DropDownList[] questionsList;
        int numOfRows;
        protected void Page_Load(object sender, EventArgs e)
        {
            yourcourseid = Request.QueryString["yourcourseid"];
            section = Request.QueryString["section"];

            SqlDataSource1.SelectCommand = "SELECT SectionQuestionID, QuestionText FROM SectionQuestions WHERE (CourseSectionID = " + section + ")";
            DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            numOfRows = dv.Table.Rows.Count;
            
            Label[] questionLabel = new Label[numOfRows];
            questionsList = new DropDownList[numOfRows];
            HtmlContainerControl[] para = new HtmlContainerControl[numOfRows*2]; //= new HtmlGenericControl("p");


            for (int i = 0; i < numOfRows; i++)
            {

                int sectionQuestionId = (int)dv.Table.Rows[i][0];
                String questionText = (String)dv.Table.Rows[i][1];

                SqlDataSource1.SelectCommand = "SELECT AnswerText, QuestionAnswerID FROM QuestionAnswers WHERE (SectionQuestionID = " + sectionQuestionId + ")";
                //DataView dv2 = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                //String questionOneOfAnswers = (String)dv2.Table.Rows[0][0];
                //String answerId = (String)dv2.Table.Rows[0][1];

                /*DropDownList1.DataSource = SqlDataSource1;
                DropDownList1.DataTextField = "AnswerText";
                DropDownList1.DataValueField = "QuestionAnswerID";
                DropDownList1.DataBind();*/
                
                //Dodanie pytania i odpowiedzi do pytania
                para[i] = new HtmlGenericControl("p");
                PlaceHolder1.Controls.Add(para[i]);
                questionLabel[i] = new Label();
                questionLabel[i].Text = questionText;
                PlaceHolder1.Controls.Add(questionLabel[i]);

                para[i*2] = new HtmlGenericControl("p");
                PlaceHolder1.Controls.Add(para[i*2]);
                questionsList[i] = new DropDownList();
                questionsList[i].DataSource = SqlDataSource1;
                questionsList[i].DataTextField = "AnswerText";
                questionsList[i].DataValueField = "QuestionAnswerID";
                questionsList[i].DataBind();
                PlaceHolder1.Controls.Add(questionsList[i]);

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Sprawdzenie testu
            
            //Sprawzenie ile odpowiedzi jest poprawnych
            SqlDataSource1.SelectCommand = "SELECT CorrectAnswer FROM SectionQuestions WHERE (CourseSectionID = " + section + ")";
            DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

            int goodAnswersCount = 0;
            for (int i = 0; i < numOfRows; i++)
            {
                int goodAnswerId = (int)dv.Table.Rows[i][0];

                if (goodAnswerId == Int32.Parse(questionsList[i].SelectedValue))
                {
                    goodAnswersCount = goodAnswersCount +1;
                }

            }
            //Pobranie daty podjęcia testu
            DateTime testTakenDate = System.DateTime.Today;

            //Pobranie ID usera
            MembershipUser myObject = Membership.GetUser();
            string StudentID = myObject.ProviderUserKey.ToString();


            //Popranie maksymalnego id z bazy wynikow

            int QuizResultID  = 0;
            SqlDataSource1.SelectCommand = "SELECT MAX(QuizResultID) FROM QuizResults";
            DataView dv2 = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

            if (dv2.Table.Rows[0][0].Equals(System.DBNull.Value))
            {
                QuizResultID = 1;
            }
            else
            {
                QuizResultID = (int)dv2.Table.Rows[0][0];
            }


            //Dodanie wyników testu do bazy danych SQL
            SqlDataSource1.InsertCommand = "INSERT INTO QuizResults (QuizResultID, CourseSectionID, Score, TotalQuestions, DateTaken, StudentID) VALUES " +
            " ('" + (QuizResultID + 1) + "','" + section + "','" + goodAnswersCount + "','" + numOfRows + "','" + testTakenDate + "','" + StudentID + "')";
            SqlDataSource1.Insert();
            //Label1.Text = "" + goodAnswersCount;

            //Przekierowanie na stronę główną kursu
            Response.Redirect("Course.aspx?yourcourseid=" + yourcourseid);
        }
    }

}
